<template>
	<view>
		<img class="img" src="../../static/images/login-bg.jpg" />
		<view class="title">进修管理系统</view>
		<view v-if="!passwordReset">
			<view class="container-radius">
				<u--form :model="form" :rules="rules" ref="uForm" label-width="80">
					<u-form-item label="登录账号" prop="username" borderBottom>
						<u--input v-model="form.username" border="none" placeholder="填写进修系统的登录帐号" />
					</u-form-item>
					<u-form-item label="登录密码" prop="password" borderBottom>
						<u--input type="password" v-model="form.password" border="none" placeholder="填写进修系统的登录密码" />
					</u-form-item>
				</u--form>
			</view>
			<view class="container-null">
				<u-button type="primary" size="default" @tap="login">登 录</u-button>
			</view>
			<view class="forget-password" @tap="toForgetPassword">忘记密码？</view>
		</view>
		<view v-else>
			<view class="container-radius">
				<u--form :model="resetForm" :rules="resetRules" ref="resetFormRef" label-width="90">
					<u-form-item label="手机号" prop="phonenumber" borderBottom>
						<u--input v-model="resetForm.phonenumber" border="none" placeholder="请输入手机号" />
					</u-form-item>
					<u-form-item label="身份证号" prop="idCard" borderBottom>
						<u--input v-model="resetForm.idCard" border="none" placeholder="请输入身份证号" />
					</u-form-item>
					<u-form-item label="新密码" prop="password" borderBottom>
						<u--input type="password" v-model="resetForm.password" border="none" placeholder="请输入新密码" />
					</u-form-item>
					<u-form-item label="确认密码" prop="confirmPassword" borderBottom>
						<u--input type="password" v-model="resetForm.confirmPassword" border="none"
							placeholder="请再次输入新密码" />
					</u-form-item>
				</u--form>
			</view>
			<view class="container-null" style="display: flex; justify-content: space-around">
				<u-button size="small" @click="passwordReset = false">返回登录</u-button>
				<u-button size="small" @click="resetFormFields">清空</u-button>
				<u-button size="small" type="primary" @click="submitResetForm">提交</u-button>
			</view>
		</view>
		<u-popup :show="show" mode="center" :round="10" @close="show = false" @open="open">
			<view class="popup-container">
				<img src="../../static/images/close.png" @click="show = false" alt="" class="close" />
				<view class="popup-content">
					<view class="popup-title">更新提示</view>
					<view class="popup-text">
						<view style="color: #424242; font-size: 27rpx">
							<text>亲爱的学员，您好!欢迎您访问华山医院教育一网通办平台-进修管理系统</text>

							<view style="margin-top: 20rpx; font-weight: bold; color: #424242">若您希望访问进修系统并<text
									style="color: #bf1b0a">提交进修申请</text>，可通过以两种方式访问系统，相关申请流程也将顺利推进</view>
							<view style="display: flex; align-items: center; margin-top: 20rpx">1.点击下方按钮 <view
									class="btnSmall"> 进修管理系统</view>
							</view>
							<text>2.复制链接至浏览器中打开</text>
							<br />
							<view class="url">
								{{ linkUrl }}
								<view class="copy" @click="copy"> 复制 </view>
							</view>
							<view class="desc">若您希望进行进修打卡报道，关闭此提示即可</view>
						</view>
					</view>
					<view class="bigBtn" @click="toLink">
						<text>进修管理系统</text>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let that;
	import config from "@/common/config";
	export default {
		data() {
			return {
				passwordReset: false,
				resetForm: {
					phonenumber: "", // 手机号
					idCard: "", // 身份证号
					password: "", // 新密码
					confirmPassword: "", // 确认密码
				},
				resetRules: {
					phonenumber: [{
							required: true,
							message: "手机号输入不能为空",
							trigger: ["blur", "change"],
						},
						{
							pattern: /^1[3-9]\d{9}$/,
							message: "请输入正确的手机号",
							trigger: ["blur", "change"],
						},
					],
					idCard: [{
							required: true,
							message: "身份证号码输入不能为空",
							trigger: ["blur", "change"],
						},
						{
							pattern: /^\d{17}[\dXx]$/,
							message: "身份证号码错误，请重新输入",
							trigger: ["blur", "change"],
						},
					],
					password: [{
						required: true,
						message: "密码不能为空",
						trigger: ["blur", "change"],
					}, ],
					confirmPassword: [{
							required: true,
							message: "密码不能为空",
							trigger: ["blur", "change"],
						},
						{
							validator: (rule, value, callback) => {
								if (value !== this.resetForm.password) {
									callback(new Error("两次密码不一致"));
								} else {
									callback();
								}
							},
							trigger: ["blur", "change"],
						},
					],
				},
				form: {
					// idCard: null,
					username: null,
					password: null,
				},
				show: false,
				linkUrl: config.linkUrl,
				rules: {
					// idCard: {
					//   required: true,
					//   message: "请填写身份证号",
					//   trigger: ["blur", "change"],
					// },
					username: {
						required: true,
						message: "请填写登录账号",
						trigger: ["blur", "change"],
					},
					password: {
						required: true,
						message: "请填写登录密码",
						trigger: ["blur", "change"],
					},
				},
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {
			that = this;
		},
		methods: {
			copy() {
				uni.setClipboardData({
					data: this.linkUrl,
					success: function() {
						that.$msg("复制成功");
					},
				});
			},
			toLink() {
				// 另外一个tab打开
				window.open(this.linkUrl, "_blank");
			},
			login() {
				that.$refs.uForm.validate().then((res) => {
					uni.$u.http.post("/au/defaultLogin/login", that.form).then((res) => {
						uni.setStorageSync(that.$config.cachePrefix + "token", res.data);
						uni.$u.http.get("/getInfo").then((result) => {
							uni.setStorageSync(that.$config.cachePrefix + "user", result.user);
							uni.setStorageSync(
								that.$config.cachePrefix + "roles",
								result.roles
							);
							uni.setStorageSync(
								that.$config.cachePrefix + "permissions",
								result.permissions
							);
							that.$msg("登录成功", function() {
								uni.$u.route("/pages/index/index");
							});
						});
					});
				});
			},
			toForgetPassword() {
				this.passwordReset = true;
				this.$nextTick(() => {
					this.$refs.resetFormRef.setRules(this.resetRules);
				});
			},
			submitResetForm() {
				this.$refs.resetFormRef
					.validate()
					.then((res) => {
						//  uni-app中没有resetPassword接口，暂时注释, 您可以自行实现
						uni.$u.http
							.post("/system/user/profile/resetPwd", this.resetForm)
							.then(() => {
								this.$msg("密码重置成功");
								this.passwordReset = false;
							})
							.catch(() => {
								this.$msg("密码重置失败，请重试");
							});
					})
					.catch((errors) => {
						this.$msg("请检查输入内容");
					});
			},
			resetFormFields() {
				this.$refs.resetFormRef.resetFields();
			},
		},
	};
</script>

<style lang="scss" scoped>
	.bigBtn {
		width: 240rpx;
		height: 80rpx;
		background-color: #bf1b0a;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		margin: 40rpx auto 20rpx auto;

		text {
			color: #fff;
		}
	}

	.btnSmall {
		margin-left: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #bf1b0a;
		color: #fff;
		height: 40rpx;
		border-radius: 8rpx;
		font-size: 22rpx;
		padding: 0 10rpx;
	}

	.url {
		margin-left: 20rpx;
		font-size: 18rpx;
		display: flex;
		align-items: center;
	}

	.desc {
		font-size: 26rpx;
		font-weight: 400;
		margin-top: 30rpx;
		font-weight: bold;
	}

	.copy {
		background-color: #dddddd;
		color: #fff;
		width: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 6rpx;
		padding: 4rpx 0;
		margin-left: 10rpx;
	}

	.popup-container {
		width: 83vw;
		position: relative;
		background-color: #fff;
		overflow: hidden;
		border-radius: 10px;
		padding: 15rpx;
		box-sizing: border-box;

		.close {
			position: absolute;
			top: 15rpx;
			right: 25rpx;
			width: 30rpx;
			height: 30rpx;
		}

		.popup-title {
			text-align: center;
			font-size: 42rpx;
			font-weight: bolder;
		}
	}

	.img {
		width: 100%;
	}

	.title {
		color: #a52529;
		font-size: 20px;
		font-weight: bolder;
		text-align: center;
		margin-top: 15px;
		margin-bottom: 15px;
	}

	.container-box {
		color: $u-main-color;
		line-height: 35px;
	}

	.forget-password {
		text-align: center;
		color: #a52529;
		font-size: 28rpx;
		margin-top: 20rpx;
	}
</style>